package test1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-11-02
 * Time: 10:25
 */
public abstract class BankCardnew{
     long cardNo;   //卡号
     String password ;//密码
     double balance ; //余额
     String name;    //用户姓名
     String uid;     //用户id
     static long baseNum; //类变量, 存储各类银行卡的起始编号
     double fee;  //跨行转账手续费率
     //空卡
     public BankCardnew() {
          this.cardNo=baseNum++;  //卡号=不同银行自动产生的编号
          this. password = "666666"; //密码 = 默认值
          this.balance = 0; //余额默认为0
     }
     //银行开户, 完成对身份证号和姓名的初始化
     public  BankCardnew(String ID,String uname) {
          this.cardNo=baseNum++;  //卡号=不同银行自动产生的编号
          this.password = "666666"; //密码 = 默认值
          this.balance = 0; //余额默认为0
          this.uid = ID;
          this.name = uname;
     }
     //开户的同时存款
     public  BankCardnew(String ID,String uname,double firstMoney) {
          cardNo=baseNum++;  //卡号=不同银行自动产生的编号
          password = "666666"; //密码 = 默认值
          this.uid = ID;
          this.name = uname;
          this.balance = firstMoney;
     }
     //抽象的转账方法, 由各银行具体实现, 因为跨行转账费率各不相同
     abstract public boolean transferMoney(BankCardnew bk,double money);

     //检查密码
      boolean checkPwdRight(String curPwd) {
        return (this.password.equals(curPwd));
     }

     //a.存款方法1
     boolean deposit(double money) throws MoneyException {
          if(checkMoney(money)) {
               this.balance += money;
               return true;
          }
          return false;
     }

     //a.存款方法2
     boolean deposit(double money,String pwd) throws MoneyException {
          if( checkPwd(pwd) && checkMoney(money)) {
               this.balance += money;
               return true;
          }
          return false;
     }

     //b.取款方法1：
     boolean drawMoney(double money) throws MoneyException {
          if( checkMoney(money) && money <= this.balance) {
               this.balance -= money;
               return true;
          } else {
               System.out.println("余额不足!");
               return false;
          }
     }

     //b.取款方法2：
     boolean drawMoney(double money,String pwd) throws MoneyException {
       if(checkMoney(money) && checkPwd(pwd) && money <= this.balance) {
            this.balance -= money;
            return true;
       } else {
            System.out.println("余额不足!");
            return false;
       }

     }

     //检查存取款数额不能为负数的方法
     boolean checkMoney(double money) throws MoneyException {
          if( money < 0) {
               throw new MoneyException("存款和取款时输入的数字不能小于 0");
          }
          return true;
     }

     //一个专门的检查密码方法来实现

     //验证密码方法
     boolean checkPwd(String pwd) {
          return pwd.length() == 6;
     }

     //c.修改密码方法1：
     boolean changePwd(String newpwd){
          if( checkPwd(newpwd)) {
               this.password = newpwd;
               return true;
          }
          return false;
     }
     //c.修改密码方法2：
     boolean changePwd(String pwd,String newpwd){
          if( checkPwd(newpwd) && (this.password.equals(pwd))) {
               this.password = newpwd;
               return true;
          }
          return false;
     }

     //f.查密码的方法：
     public String getPassword() {
          return password;
     }

     public void setPassword(String password) {
          this.password = password;
     }
     //d.查询余额方法
     public double getBalance() {
          return balance;
     }

     //e.查姓名方法
     public String getName() {
          return name;
     }

     public long getCardNo() {
          return cardNo;
     }

     public void setCardNo(long cardNo) {
          this.cardNo = cardNo;
     }

     public void setBalance(double balance) {
          this.balance = balance;
     }

     public void setName(String name) {
          this.name = name;
     }

     public String getUid() {
          return uid;
     }

     public void setUid(String uid) {
          this.uid = uid;
     }

     public static long getBaseNum() {
          return baseNum;
     }

     public static void setBaseNum(long baseNum) {
          BankCardnew.baseNum = baseNum;
     }

     public double getFee() {
          return fee;
     }

     public void setFee(double fee) {
          this.fee = fee;
     }

     @Override
     public String toString() {
          return "BankCardnew{" +
                  "卡号=" + cardNo +
                  ", 密码='" + password + '\'' +
                  ", 余额=" + balance +
                  ", 用户姓名='" + name + '\'' +
                  ", 用户id='" + uid + '\'' +
                  ", 费率=" + fee +
                  '}';
     }
}
